package com.zcdyy.rentalofhouse.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zcdyy.rentalofhouse.R;
import com.zcdyy.rentalofhouse.base.BaseFragment;
import com.zcdyy.rentalofhouse.listener.OnItemClickListener;
import com.zcdyy.rentalofhouse.ui.activity.SaleActivity;
import com.zcdyy.rentalofhouse.ui.activity.ShopsActivity;
import com.zcdyy.rentalofhouse.ui.activity.SingleRentActivity;
import com.zcdyy.rentalofhouse.ui.activity.WholeRentDetailsActivity;
import com.zcdyy.rentalofhouse.ui.adapter.ReleaseMessAdapter;
import com.zcdyy.rentalofhouse.utils.DividerItemDecoration;
import com.zcdyy.rentalofhouse.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 房屋租赁
 * Created by chuan on 2017/3/9.
 */

public class RentalFragment extends BaseFragment {
    private TextView title;
    private RelativeLayout back;
    private RecyclerView recyclerView;
    private ReleaseMessAdapter adapter;
    private List<String> list = new ArrayList<>();
    private Class[] cls = {WholeRentDetailsActivity.class, SaleActivity.class, SingleRentActivity.class, ShopsActivity.class};
    @Override
    protected View onCreateViews(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_rental, container, false);
        findViewsByIds(view);
        initData();
        bind();
        return view;
    }

    private void initData() {
        title.setText("发布");
        back.setVisibility(View.GONE);
        list.add("整租");
        list.add("二手房");
        list.add("单间出租");
        list.add("商铺");
    }

    private void bind() {
        adapter = new ReleaseMessAdapter(getActivity(), list);
        //recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        DividerItemDecoration decoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST);
        decoration.setDivider(R.drawable.divid_shape);
        recyclerView.addItemDecoration(decoration);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity(), cls[position]);
                intent.putExtra("type", list.get(position));
                startActivity(intent);
            }
        });
    }

    private void findViewsByIds(View view) {
        title = Utils.findViewsById(view, R.id.top_tv_title);
        back = Utils.findViewsById(view, R.id.top_rl_back);
        recyclerView = Utils.findViewsById(view, R.id.recycler);
    }
}
